package com.carleasoft.mps.cms.business.train.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carleasoft.mps.cms.business.article.constants.ExceptionCMSEnum;
import com.carleasoft.mps.cms.business.drug.bean.entity.DrugOilManage;
import com.carleasoft.mps.cms.business.train.bean.entity.TrainContent;
import com.carleasoft.mps.cms.business.train.bean.entity.TrainScoreContent;
import com.carleasoft.mps.cms.business.train.bean.po.TrainContentPo;
import com.carleasoft.mps.cms.business.train.service.TrainContentService;
import com.carleasoft.mps.cms.business.train.service.TrainScoreContentService;
import com.carleasoft.mps.core.exceptions.BusinessException;
import com.carleasoft.mps.core.result.R;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import com.carleasoft.mps.core.utils.IBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: yangHao
 * @createDate:2023/6/30
 * @description:
 */
@RestController
@RequestMapping("content")
public class TrainContentController {
    @Autowired
    private TrainContentService trainContentService;
    @Autowired
    private TrainScoreContentService trainScoreContentService;



    /**
     * 添加考核内容
     *
     * @param trainContent
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody TrainContent trainContent) {
        List<TrainContent> trainContents = trainContentService.list(Wrappers.<TrainContent>lambdaQuery()
                .eq(TrainContent::getExamProject, trainContent.getExamProject()));
        if (CollUtil.isNotEmpty(trainContents)){
            throw new BusinessException(ExceptionCMSEnum.NO_CONTENT_EXITS);
        }
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        trainContent.setCreateTime(LocalDateTime.now());
        trainContent.setCreatePersonCode(user.getUserCode());
        trainContent.setCreatePersonName(user.getUserName());
        trainContent.setBusinessCode(IdWorker.getIdStr());
        return R.ok(trainContentService.save(trainContent));
    }

    /**
     * 修改考核内容
     *
     * @param trainContent
     * @return
     */
    @PostMapping("/edit")
    public R update(@RequestBody TrainContent trainContent) {
        IBeanUtil.paramVerify(trainContent, "businessCode");
        List<TrainContent> trainContents = trainContentService.list(Wrappers.<TrainContent>lambdaQuery()
                .eq(TrainContent::getExamProject, trainContent.getExamProject())
        .ne(TrainContent::getBusinessCode,trainContent.getBusinessCode()));
        if (CollUtil.isNotEmpty(trainContents)){
            throw new BusinessException(ExceptionCMSEnum.NO_CONTENT_EXITS);
        }
        TrainContent one = trainContentService.getOne(Wrappers.<TrainContent>lambdaQuery()
                .eq(TrainContent::getBusinessCode, trainContent.getBusinessCode()));
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        BeanUtil.copyProperties(trainContent, one);
        one.setCreateTime(LocalDateTime.now());
        one.setCreatePersonCode(user.getUserCode());
        one.setCreatePersonName(user.getUserName());
        one.setBusinessCode(IdWorker.getIdStr());
        return R.ok(trainContentService.updateById(one));
    }


    /**
     * 分页
     *
     * @param trainContentPo
     * @return
     */
    @PostMapping("/pageQuery")
    public R pageQuery(@RequestBody TrainContentPo trainContentPo) {
        Page page = new Page(trainContentPo.getCurrent(), trainContentPo.getSize());
        QueryWrapper<TrainContent> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(trainContentPo.getExamProject())) {
            queryWrapper.like("EXAM_PROJECT", trainContentPo.getExamProject());
        }
        queryWrapper.orderByDesc("CREATE_TIME");
        return R.ok(trainContentService.page(page, queryWrapper));
    }

    /**
     * 列表
     *
     * @return
     */
    @PostMapping("/select")
    public R select() {
        return R.ok(trainContentService.list());
    }


    /**
     * 删除
     *
     * @param trainContent
     * @return
     */
    @PostMapping("/delete")
    public R delete(@RequestBody TrainContent trainContent) {
        TrainContent trainContentServiceOne = trainContentService.getOne(Wrappers.<TrainContent>lambdaQuery()
                .eq(TrainContent::getBusinessCode, trainContent.getBusinessCode()));
        int count = trainScoreContentService.count(Wrappers.<TrainScoreContent>lambdaQuery()
                .eq(TrainScoreContent::getExamProject, trainContentServiceOne.getExamProject()));
        if (count>0){
            throw new BusinessException(ExceptionCMSEnum.EXITS_NO);
        }
        return R.ok(trainContentService.remove(Wrappers.<TrainContent>lambdaQuery()
                .eq(TrainContent::getBusinessCode, trainContent.getBusinessCode())));
    }

}
